#include "mainpage.h"
#include "ui_mainpage.h"
#include "netprotocol.h"
#include "netdriver.h"
#include "datatemp.h"
#include <QMessageBox>
#include <QColorDialog>
#include <QDebug>
#include <QDateTime>
#include <QJsonArray>
#include <QJsonObject>
#include <QPainter>
#include <QPen>
#include <QBrush>
#include <QPixmap>
#include <QImage>
#include "waterprogressbar.h"

MainPage::MainPage(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::MainPage)
  ,tem1(new WaterProgressBar)
  //,tem2(new WaterProgressBar)
  ,hum1(new WaterProgressBar)
 // ,hum2(new WaterProgressBar)
  ,slidernumhum2(new CustomSliderNum)
  ,slidernumlight(new CustomSliderNum)
  ,slidernumpress(new CustomSliderNum)
//  ,slidernumPm2_5(new CustomSliderNum)
{


    ui->setupUi(this);

     ui->btnWIFI->setStyleSheet("border: none; background-color: none;color: white;");
     ui->btnBack->setStyleSheet("border: none; background-color: none;color: white;");
     ui->btnBack_2->setStyleSheet("border: none; background-color: none;color: white;");
     ui->btnTodayMessage->setStyleSheet("border: none; background-color: none;color: white;");
     ui->btnShowAllMessage->setStyleSheet("border: none; background-color: none;color: white;");
     ui->cbIndexName->setStyleSheet("background-color: rgb(80,192,228);");
     ui->listWidget->setStyleSheet("background-color: rgb(80,191,228);");
      ui->btnwater1->setStyleSheet("border: none; background-color: none;color: white;");
       ui->btnwater2->setStyleSheet("border: none; background-color: none;color: white;");

       tem1-> setGeometry(600,-55,130, 400);//设置大小
       tem1->setColor(255,0,0,230);
       tem1->setUint("℃");
       tem1->setParent(ui->pageMain); //设置父窗口

//       tem2-> setGeometry(600, -55,100, 300);//设置大小
//       tem2->setColor(255,0,0,230);
//       tem2->setUint("℃");
//       tem2->setParent(ui->pageMain); //设置父窗口

       hum1-> setGeometry(850, -55,130, 400);//设置大小
       hum1->setColor(100,180,250,100);
       hum1->setUint("%");
       hum1->setParent(ui->pageMain); //设置父窗口

//       hum2-> setGeometry(850, -55,100, 300);//设置大小
//       hum2->setColor(100,180,250,230);
//       hum2->setUint("%");
//       hum2->setParent(ui->pageMain); //设置父窗口

       slidernumhum2->setDisabled(true);
       slidernumhum2->setOrientation(Qt::Horizontal);  // 水平方向
       slidernumhum2->setMinimum(0);  // 最小值
       slidernumhum2->setMaximum(100);  // 最大值
       slidernumhum2->setSingleStep(1);  // 步长
       slidernumhum2->setFixedSize(250, 70); // 设置固定宽高，高度设置大一些，让数字显示出来
       slidernumhum2->setGeometry(700,310,300,300);
       slidernumhum2->setValue(80);
       slidernumhum2->setMagnification(15);
       slidernumhum2->updateCurrentSliderValueSite();
       slidernumhum2->setParent(ui->pageMain);

       slidernumpress->setDisabled(true);
       slidernumpress->setOrientation(Qt::Horizontal);  // 水平方向
       slidernumpress->setMinimum(0);  // 最小值
       slidernumpress->setMaximum(100);  // 最大值
       slidernumpress->setSingleStep(1);  // 步长
       slidernumpress->setFixedSize(250, 70); // 设置固定宽高，高度设置大一些，让数字显示出来
       slidernumpress->setGeometry(700,435,300,300);
       slidernumpress->setValue(80);
       slidernumpress->setMagnification(1);
       slidernumpress->updateCurrentSliderValueSite();
       slidernumpress->setParent(ui->pageMain);

       slidernumlight->setDisabled(true);
       slidernumlight->setOrientation(Qt::Horizontal);  // 水平方向
       slidernumlight->setMinimum(0);  // 最小值
       slidernumlight->setMaximum(100);  // 最大值
       slidernumlight->setSingleStep(1);  // 步长
       slidernumlight->setFixedSize(250, 70); // 设置固定宽高，高度设置大一些，让数字显示出来
       slidernumlight->setGeometry(700,557,300,300);
       slidernumlight->setValue(80);
       slidernumlight->setMagnification(10);
       slidernumlight->updateCurrentSliderValueSite();
       slidernumlight->setParent(ui->pageMain);

//       slidernumPm2_5->setDisabled(true);
//       slidernumPm2_5->setOrientation(Qt::Horizontal);  // 水平方向
//       slidernumPm2_5->setMinimum(0);  // 最小值
//       slidernumPm2_5->setMaximum(100);  // 最大值
//       slidernumPm2_5->setSingleStep(1);  // 步长
//       slidernumPm2_5->setFixedSize(250, 70); // 设置固定宽高，高度设置大一些，让数字显示出来
//       slidernumPm2_5->setGeometry(700,550,300,300);
//       slidernumPm2_5->setValue(80);
//       slidernumPm2_5->setMagnification(7);
//       slidernumPm2_5->updateCurrentSliderValueSite();
//       slidernumPm2_5->setParent(ui->pageMain);


      // ui->lbTem1Name->setStyleSheet("color: blue;");
       ui->lbTem2Name->setStyleSheet("color: blue;");
    //   ui->lbHum1Name->setStyleSheet("color: blue;");
       ui->lbHum2Name->setStyleSheet("color: blue;");
       ui->lbCo2Name->setStyleSheet("color: blue;");
    //   ui->lbuvName->setStyleSheet("color: blue;");
       ui->lblightName->setStyleSheet("color: blue;");
       ui->lbPm2_5Name->setStyleSheet("color: blue;");
        ui->lbwater1->setStyleSheet("color: white;");
         ui->lbwater2->setStyleSheet("color: white;");
         //ui->lbboardnum->setStyleSheet("color: white;");

    ui->stackedWidget->setCurrentWidget(ui->pageLogin);
    ui->lePassWord->setEchoMode(QLineEdit::Password);
    netDriver::getObject()->netConnect(dataTemp::getObject()->serverIp,dataTemp::getObject()->serverPort);
    ui->leUserName->setText(dataTemp::getObject()->username);
    ui->lePassWord->setText(dataTemp::getObject()->password);
    ui->lbShowServerIp->setText(QString("serverip:%1").arg(dataTemp::getObject()->serverIp));
    ui->lbShowServerPort->setText(QString("serverport:%1").arg(dataTemp::getObject()->serverPort));
   // ui->lbRGB->setStyleSheet(QString("background-color:rgb(255,255,255);"));

    connect(netProtocol::getObject(),&netProtocol::LoginFeedBack,this,&MainPage::LoginFeedBackSlot);
    connect(netProtocol::getObject(),&netProtocol::updateMyDeviceName,this,&MainPage::updateMyDeviceNameSlot);
    connect(netProtocol::getObject(),&netProtocol::showDodayMessage,this,&MainPage::showDodayMessageSlot);
    connect(netProtocol::getObject(),&netProtocol::realTimeMessage,this,&MainPage::realTimeMessageSlot);
//    QDateTime now=QDateTime::currentDateTime();
//    now=now.addDays(-1);
//   qDebug() << now.toString("yyyy-MM-dd hh:mm:ss");
    tem1->setVaule(20.32);
//    tem2->setVaule(21.78);
    hum1->setVaule(50);
//    hum2->setVaule(51);
    slidernumhum2->setValue(980/15);
    slidernumhum2->updateCurrentSliderValueSite();
    slidernumpress->setValue(4);
    slidernumpress->updateCurrentSliderValueSite();
    slidernumlight->setValue(240/10);
    slidernumlight->updateCurrentSliderValueSite();
//    slidernumPm2_5->setValue(530/7);
//    slidernumPm2_5->updateCurrentSliderValueSite();
//  ui->stackedWidget->setCurrentWidget(ui->pageMain);
}

MainPage::~MainPage()
{
    delete ui;
}

void MainPage::on_btnLogin_clicked()
{
    netProtocol::getObject()->userfLogin(ui->leUserName->text(),ui->lePassWord->text());
}

void MainPage::LoginFeedBackSlot(bool re)
{
 if(re)
 {
    QMessageBox::information(this,"info","登录成功!");
    ui->stackedWidget->setCurrentWidget(ui->pageMain);

 }
 else {
     QMessageBox::information(this,"info","登录失败!");
      }
}

void MainPage::updateMyDeviceNameSlot(const QJsonArray &arr)
{
 for(int i =0;i<arr.size();i++)
 {
   ui->cbIndexName->addItem(arr.at(i).toObject().value("device_id").toString());
 }
}

void MainPage::showDodayMessageSlot(const QJsonArray &arr)
{
for(int i = 0;i<arr.size();i++)
{
   QJsonObject obj= arr.at(i).toObject();
   //obj.value("")
            QString title ="boardid  tem    hum1    light  press   hum2  water1  water2  time";
   QString message=QString(" %1     %2     %3     %4     %5     %6     %7   %8    %9")
           .arg(obj.value("boardid").toInt())
           .arg(obj.value("tem").toDouble())
           .arg(obj.value("hum1").toDouble())
           .arg(obj.value("light").toDouble())
           .arg(obj.value("press").toDouble())
           .arg(obj.value("hum2").toDouble())
           .arg(obj.value("water1").toBool())
            .arg(obj.value("water2").toBool())
            .arg(obj.value("time").toString())
          ;

    ui->listWidget->addItem(title);
    ui->listWidget->addItem(message);

}
}

void MainPage::realTimeMessageSlot(const QJsonObject &obj)

{
    int co2=obj.value("co2").toInt();
    int uv= obj.value("uv").toInt();
   double te1= obj.value("te1").toDouble();
     double hu1= obj.value("hu1").toDouble();
    int light= obj.value("light").toInt();
    bool methane= obj.value("methane").toBool();
   bool fire= obj.value("fire").toBool();
  bool smoke=  obj.value("smoke").toBool();
  bool inf=  obj.value("inf").toBool();
   int pm2_5= obj.value("pm2_5").toInt();
   double te2= obj.value("te2").toDouble();
   double hu2= obj.value("hu2").toDouble();
   bool correlation= obj.value("correlation").toBool();
   bool reflex= obj.value("reflex").toBool();
   QString time= obj.value("time").toString();
tem1->setVaule(te1);
//tem2->setVaule(te2);
hum1->setVaule(hu1);
//hum2->setVaule(hu2);
//slidernum->setValue(co2/15);
//slidernum->updateCurrentSliderValueSite();
//slidernumnv->setValue(uv);
//slidernumnv->updateCurrentSliderValueSite();
slidernumlight->setValue(light/10);
slidernumlight->updateCurrentSliderValueSite();
//slidernumPm2_5->setValue(pm2_5/7);
//slidernumPm2_5->updateCurrentSliderValueSite();
//if(methane && fire && smoke )
//{
//      QPixmap pixmap(":/Image/Util/alarmoff.png");
//      ui->btnFireAlarm->setIcon(QIcon(pixmap));
//    //netProtocol::getObject()->controlNormalDevice(ui->cbIndexName->currentText(),1,false);
//    netProtocol::getObject()->controlNormalDevice(ui->cbIndexName->currentText(),2,false);
//}

}

void MainPage::controlNormalDeviceSlot()
{
    QPushButton *btn = qobject_cast<QPushButton *>(sender());
    QString  senderName=btn->objectName();
//    if(senderName == ui->btnServo->objectName())
//    {
//      if(ui->btnServo->text()=="打开窗")
//      {
//          netProtocol::getObject()->controlNormalDevice(ui->cbIndexName->currentText(),3,true);
//          ui->btnServo->setText("关闭窗");
//          qDebug() << "zaide";

//      }

}

void MainPage::on_btnTodayMessage_clicked()
{ ui->stackedWidget->setCurrentWidget(ui->page1);
    netProtocol::getObject()->requestMessage(0);
}

void MainPage::on_cbIndexName_currentIndexChanged(const QString &arg1)
{
  dataTemp::getObject()->currentDeviceId=arg1;
  qDebug() << "currentdeviceId" << dataTemp::getObject()->currentDeviceId;
}

void MainPage::paintEvent(QPaintEvent *event)
{
    if(ui->stackedWidget->currentWidget()==ui->pageLogin)
    {
    QPainter painter;
    painter.begin(this);
    QImage img2(":/4.png");
    QImage imgd = img2.scaled(this->width(),this->height(),Qt::IgnoreAspectRatio);
    painter.drawImage(0,0,imgd);
    painter.end();
    }
    if(ui->stackedWidget->currentWidget()==ui->pageMain ||ui->stackedWidget->currentWidget()==ui->page1 )
    {
    QPainter painter;
    painter.begin(this);
    QImage img2(":/5 .png");
    QImage imgd = img2.scaled(this->width(),this->height(),Qt::IgnoreAspectRatio);
    painter.drawImage(0,0,imgd);
    painter.end();
    }
}



void MainPage::on_btnBack_clicked()
{
    ui->stackedWidget->setCurrentWidget(ui->pageMain);
    ui->listWidget->clear();
}


void MainPage::on_btnShowAllMessage_clicked()
{
    ui->stackedWidget->setCurrentWidget(ui->page1);
    netProtocol::getObject()->requestMessage(0);
}

void MainPage::on_btnwater1_clicked()
{
    QPixmap pixmap(":/Image/Control/switch_close.png");
    QPixmap pixmap2(":/Image/Control/switch_open.png");
    if(water1flag ==false)
       { ui->btnwater1->setIcon(pixmap2);
    ui->btnwater1->setIconSize(QSize(120,120));
    water1flag=true;}
    else {
        ui->btnwater1->setIcon(pixmap);
            ui->btnwater1->setIconSize(QSize(120,120));
            water1flag=false;
    }
}

void MainPage::on_btnwater2_clicked()
{
    QPixmap pixmap(":/Image/Control/switch_close.png");
    QPixmap pixmap2(":/Image/Control/switch_open.png");
    if(water2flag ==false)
       { ui->btnwater2->setIcon(pixmap2);
    ui->btnwater2->setIconSize(QSize(120,120));
    water2flag=true;}
    else {
        ui->btnwater2->setIcon(pixmap);
            ui->btnwater2->setIconSize(QSize(120,120));
            water2flag=false;
    }
}
